using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace LMSData
{
    
    public partial class UserController
    {
        
        public UserCollection FetchAll(bool Active)
        {
            UserCollection coll = new UserCollection();
            Query qry = new Query(User.Schema);
            qry.AddWhere(LMSData.User.Columns.IsDeleted, !Active);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
     
        }

        public User GetByID(int UserID)
        {
            User usr = new User();
            UserCollection coll = new UserCollection();
            Query qry = new Query(User.Schema);
            qry.AddWhere(LMSData.User.Columns.UserID, UserID);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            if (coll.Count > 0) usr = coll[0];
            return usr;
        }

        public int GetUserID(string UserName)
        {
            UserCollection uc = new UserCollection();
            Query uq = new Query(User.Schema).WHERE(User.Columns.Username, UserName).WHERE(User.Columns.IsDeleted, false);
            uc.LoadAndCloseReader(uq.ExecuteReader());

            int UserID = -1;

            if (uc.Count > 0)
                UserID = uc[0].UserID;

            return UserID;
        }

        public User FetchByName(string Username)
        {
            UserCollection uc = new UserCollection();
            Query uq = new Query(User.Schema).WHERE(User.Columns.Username, Username).WHERE(User.Columns.IsDeleted, false);
            uc.LoadAndCloseReader(uq.ExecuteReader());

            if (uc.Count > 0)
                return uc[0];

            return new User();
        }

        public static GroupCollection GetGroups(string UserID)
        {
            UserGroupMapCollection ugmc = new UserGroupMapCollection();
            Query qry = new Query(UserGroupMap.Schema).WHERE(UserGroupMap.Columns.UserID, UserID);
            ugmc.LoadAndCloseReader(qry.ExecuteReader());

            GroupCollection coll = new GroupCollection();
            foreach (UserGroupMap ugm in ugmc)
                coll.Add(new Group(ugm.GroupID));
            return coll;
        }

        public static CourseCollection GetCourses(string UserID)
        {
            UserCourseMapCollection ucmc = new UserCourseMapCollection();
            Query qry = new Query(UserCourseMap.Schema).WHERE(UserCourseMap.Columns.UserID, UserID);
            ucmc.LoadAndCloseReader(qry.ExecuteReader());

            CourseCollection coll = new CourseCollection();
            foreach (UserCourseMap ucm in ucmc)
            {
                Course crs = new Course(ucm.CourseID);
                if (!crs.IsDeleted)
                    coll.Add(crs);
            }
            return coll;
        }
    }
}
